package com.jx.mst.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jx.mst.model.common.enums.AppHttpCodeEnum;
import com.jx.mst.model.common.utils.AppJwtUtil;
import com.jx.mst.model.user.dtos.UserDtos;
import com.jx.mst.model.common.dtos.ResponseResult;
import com.jx.mst.model.user.reps.UserReps;
import com.jx.mst.user.dao.UsUserDao;
import com.jx.mst.user.entity.UsUser;
import com.jx.mst.user.service.UsUserService;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.jx.mst.model.user.constant.UserCon.USER_LEVEL_ADMIN;
import static com.jx.mst.model.common.enums.AppHttpCodeEnum.DATA_NOT_EXIST;
import static com.jx.mst.model.user.constant.UserCon.USER_STATE_DISABLE;

/**
 * (UsUser)表服务实现类
 *
 * @author makejava
 * @since 2023-12-23 04:16:45
 */
@Service("usUserService")
public class UsUserServiceImpl extends ServiceImpl<UsUserDao, UsUser> implements UsUserService {


    @Override
    public ResponseResult adminLogin(UserDtos userDtos) {
        List<UsUser> usUserList = query().eq(StrUtil.isNotBlank(userDtos.getUsername()), "user_name", userDtos.getUsername()).list();
        if(list() ==null || usUserList.isEmpty()){
            return ResponseResult.errorResult(DATA_NOT_EXIST,"用户信息不存在");
        }
        UsUser usUser = usUserList.get(0);
        if(usUser.getUserLevel()!=USER_LEVEL_ADMIN){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_ADMIND);
        }
        if(usUser.getUserState()==USER_STATE_DISABLE){
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_DIS);
        }
        if(!usUser.getUserPassword().equals(userDtos.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_DIS);
        }

        String token = AppJwtUtil.getToken(usUser.getId().longValue());
        UserReps userReps = BeanUtil.copyProperties(usUser, UserReps.class);
        userReps.setToken(token);

        return ResponseResult.okResult(userReps);
    }
}

